package com.lin.mr.maizilaundry.fragmentmain;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.activity.HomeActivity;
import com.lin.mr.maizilaundry.entity.Laundry;
import com.lin.mr.maizilaundry.myadapter.BasketAdapter;
import com.lin.mr.maizilaundry.myapplication.MyApplication;
import com.lin.mr.maizilaundry.utils.CountEvent;

import de.greenrobot.event.EventBus;

/**
 * Created by lenovog on 2016/7/6.
 */
public class FragmentBasket extends Fragment{
    private ImageView iv_back_basket;
    private ImageView iv_basket; //空篮子
    //list_laundry是一个大的ListView，包含了衣服订单列表和优惠券
    private ListView list_laundry;
    private TextView tv_clear;
    private View view;
    private View view_coupon;
    private TextView previous_total,after_total;
    private BasketAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_basket,container,false);
        initView();
        //删除订单
        tv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (MyApplication.laundries.size()!=0){
                    MyApplication.laundries.removeAll(MyApplication.laundries);
                    adapter.notifyDataSetChanged();//更新数据
                }else {
                    return;
                }
            }
        });

        //返回键的点击事件，返回到首页
        iv_back_basket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomeActivity activity = (HomeActivity) getActivity();
                activity.unLoginForBack();
            }
        });

        calculate();//计算优惠后的价格
        adapter = new BasketAdapter(getActivity(),MyApplication.laundries,
                view_coupon,iv_basket);
        list_laundry.setAdapter(adapter);
        iv_basket.setVisibility(View.INVISIBLE);
        EventBus.getDefault().register(this);//注册EventBus
        return view;
    }

    //计算价格
    public void calculate() {
        int sum = 0;//合计的费用
        for (int i = 0; i < MyApplication.laundries.size(); i++) {
            Laundry laundry = MyApplication.laundries.get(i);
            int value = Integer.parseInt(laundry.getValue().substring(1));//下标为0的字符是人民币符号
            int amount = Integer.parseInt(laundry.getAmount());
            sum = sum +value * amount;
            previous_total.setText("¥"+sum);
            //计算优惠后的金额
            if ((sum-30)>0){
                after_total.setText("¥"+(sum-30));
            }else {
                after_total.setText("¥"+0);
            }
        }
    }
    //接收EventBus信息
    public void onEventMainThread(CountEvent event){
        int count = event.getCount();
        if (count==4){
            //下面的list_laundry应该可以去掉的
            if (MyApplication.laundries.size()==0 && list_laundry != null){
                iv_basket.setVisibility(View.VISIBLE);
            }else if (MyApplication.laundries.size() != 0){
                iv_basket.setVisibility(View.GONE);
                calculate();
            }
            list_laundry.setAdapter(adapter);
        }else if (count==5){
            Log.e("FragmentBasket","calculate");
            calculate();
        }
    }

    private void initView() {
        //这里的view写成了全局变量，onCreateView中初始化
        iv_back_basket = (ImageView) view.findViewById(R.id.iv_back_basket);
        iv_basket = (ImageView) view.findViewById(R.id.iv_basket);
        list_laundry = (ListView) view.findViewById(R.id.lv_laundry);
        tv_clear = (TextView) view.findViewById(R.id.tv_clear);

        view_coupon = View.inflate(getContext(),R.layout.fragment_basket_coupon,null);
        previous_total = (TextView) view_coupon.findViewById(R.id.previous_total);
        after_total = (TextView) view_coupon.findViewById(R.id.after_total);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);//反注册EventBus
    }
}
